/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.use.core.model;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.dltk.core.DLTKCore;
import org.eclipse.dltk.core.DLTKLanguageManager;
import org.eclipse.dltk.core.IModelElement;
import org.eclipse.dltk.core.IScriptProject;
import org.eclipse.dltk.core.search.IDLTKSearchConstants;
import org.eclipse.dltk.core.search.IDLTKSearchScope;
import org.eclipse.dltk.core.search.SearchEngine;
import org.eclipse.dltk.core.search.SearchMatch;
import org.eclipse.dltk.core.search.SearchParticipant;
import org.eclipse.dltk.core.search.SearchPattern;
import org.eclipse.dltk.core.search.SearchRequestor;
import org.eclipse.dltk.core.search.TypeNameMatchRequestor;

public class USModelHelper {

	public static class LOGSearchRequestor extends SearchRequestor {

		@Override
		public void acceptSearchMatch(SearchMatch match) throws CoreException {
			System.out.println(match);
			if (match != null) {
				System.out.println(match.getElement());
			}
		}

	}

	public static void searchVariableDeclaration(IModelElement parent,
			String patternString, SearchRequestor requestor) {

		IDLTKSearchScope scope = SearchEngine.createSearchScope(parent);

		try {
			SearchEngine engine = new SearchEngine();
			SearchPattern pattern = SearchPattern.createPattern(patternString,
					IDLTKSearchConstants.REFERENCES,
					IDLTKSearchConstants.DECLARATIONS,
					SearchPattern.R_EXACT_MATCH,
					DLTKLanguageManager.getLanguageToolkit(parent));
			engine.search(pattern, new SearchParticipant[] { SearchEngine
					.getDefaultSearchParticipant() }, scope, requestor, null);
		} catch (CoreException e) {
			if (DLTKCore.DEBUG)
				e.printStackTrace();
		}
	}

	public static void searchMethodDeclarations(IModelElement element,
			String patternString, SearchRequestor requestor) {

		IDLTKSearchScope scope = SearchEngine.createSearchScope(element);

		try {
			SearchEngine engine = new SearchEngine();
			SearchPattern pattern = SearchPattern.createPattern(patternString,
					IDLTKSearchConstants.METHOD,
					IDLTKSearchConstants.DECLARATIONS,
					SearchPattern.R_PATTERN_MATCH,
					DLTKLanguageManager.getLanguageToolkit(element));
			engine.search(pattern, new SearchParticipant[] { SearchEngine
					.getDefaultSearchParticipant() }, scope, requestor, null);
		} catch (CoreException e) {
			if (DLTKCore.DEBUG)
				e.printStackTrace();
		}
	}

	public static void searchTypeDeclarations(IScriptProject project,
			String patternString, TypeNameMatchRequestor requestor) {
		IDLTKSearchScope scope = SearchEngine.createSearchScope(project);
		try {
			SearchEngine engine = new SearchEngine();
			engine.searchAllTypeNames(
					null,
					0,
					patternString.toCharArray(),
					SearchPattern.R_EXACT_MATCH | SearchPattern.R_PATTERN_MATCH,
					IDLTKSearchConstants.TYPE, scope, requestor,
					IDLTKSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
		} catch (CoreException e) {
			if (DLTKCore.DEBUG)
				e.printStackTrace();
		}
	}

}
